package com.iraeta.service.order.impl;

import cn.hutool.core.util.StrUtil;
import com.iraeta.entity.PermissionType;
import com.iraeta.entity.User;
import com.iraeta.entity.order.GiftOrder;
import com.iraeta.entity.resulttype.GiftOrderRT;
import com.iraeta.entity.resulttype.GiftOrderTs;
import com.iraeta.entity.type.GiftOrderStatus;
import com.iraeta.entity.wx.Wxtextmessage;
import com.iraeta.mapper.order.GiftOrderMapper;
import com.iraeta.mapper.sys.UserMapper;
import com.iraeta.mapper.wx.WxtextmessageMapper;
import com.iraeta.service.order.IGiftOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 礼品订单表 服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2023-12-22
 */
@Service
public class GiftOrderServiceImpl extends ServiceImpl<GiftOrderMapper, GiftOrder> implements IGiftOrderService {
    @Resource
    UserMapper userMapper;
    @Resource
    WxtextmessageMapper wxtextmessageMapper;

    @Override
    public GiftOrderRT getGiftOrder(Integer id) {
        return baseMapper.getGiftOrder(id);
    }

    @Override
    public List<GiftOrderRT> listSelfGiftOrder(Integer createUserId, String orderno, String giftName) {
        return baseMapper.listSelfGiftOrder(createUserId, orderno, giftName);
    }

    @Override
    public GiftOrderTs getTsGiftOrder(Integer id) {
        return baseMapper.getTsGiftOrder(id);
    }

    @Override
    public List<GiftOrderTs> listSelfTsGiftOrder(Integer createUserId, String orderno) {
        return baseMapper.listSelfTsGiftOrder(createUserId, orderno);
    }

    @Transactional
    @Override
    public void submitGiftOrder(Integer id) {
        GiftOrder giftOrder = new GiftOrder();
        giftOrder.setId(id);
        giftOrder.setStatusId(GiftOrderStatus.submit);
        baseMapper.updateById(giftOrder);

        GiftOrderRT giftOrderForDb = baseMapper.getGiftOrder(id);
        int permission = giftOrderForDb.getGiftId() > 0 ? PermissionType.order_shenHe : PermissionType.order_shenHe2;
        List<User> userList = userMapper.listUserByPermissionId(permission);
        List<String> userIdList = new ArrayList<>();
        for (User user : userList) {
            if(!StrUtil.isEmpty(user.getWxuid())){
                userIdList.add(user.getWxuid());
            }
        }
        if(userIdList.size() > 0){
            String content = "您有新的礼品订单审核,提报人[" + giftOrderForDb.getCreateusername() + "],订单号[" + giftOrderForDb.getOrderNo() + "]";
            String touser = String.join("|", userIdList);
            Wxtextmessage wxtextmessage = new Wxtextmessage(touser, content);
            wxtextmessageMapper.insert(wxtextmessage);
        }
    }

    /*审核相关 查询待审核的正常订单数据*/
    @Override
    public List<GiftOrderRT> listDaiShenHeGiftOrder(  String orderno, String giftName,String createusername) {
        return baseMapper.listDaiShenHeGiftOrder(GiftOrderStatus.submit, orderno, giftName, createusername);
    }
    /*审核相关 查询待审核的特殊订单数据*/
    @Override
    public List<GiftOrderTs> listDaiShenHeTSGiftOrder(String orderno, String createusername) {
        return baseMapper.listDaiShenHeTSGiftOrder(GiftOrderStatus.submit, orderno, createusername);
    }

    @Transactional
    @Override
    public void shenHeOrder(GiftOrder giftOrder) {
        baseMapper.updateById(giftOrder);
        GiftOrderRT giftOrderForDb = baseMapper.getGiftOrder(giftOrder.getId());
        String touser = userMapper.selectById(giftOrderForDb.getCreateUserid()).getWxuid();
        if(!StrUtil.isEmpty(touser)){
            String shenHePassStr = "";
            if(giftOrder.getStatusId() == GiftOrderStatus.shenHePass){
                shenHePassStr="审核通过";
            }
            if(giftOrder.getStatusId() == GiftOrderStatus.shenHeFail){
                shenHePassStr="审核不通过";
            }
            String content = "礼品订单号[" + giftOrderForDb.getOrderNo() + "]" + shenHePassStr + ",审核人[" + giftOrderForDb.getShenheusername()+"]";
            Wxtextmessage wxtextmessage = new Wxtextmessage(touser, content);
            wxtextmessageMapper.insert(wxtextmessage);
        }
    }

    /*审核相关 查看审核历史*/
    @Override
    public List<GiftOrderRT> listShenHeHistory(Integer shenheuserid, String orderno, String createusername) {
        return baseMapper.listShenHeHistory(shenheuserid, orderno, createusername);
    }
}
